package com.jiyinit.web.core.mobile.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.web.core.mobile.dto.BlueWallRequest;
import com.jiyinit.web.core.mobile.entity.TabCoreBlueWallEntity;
import com.jiyinit.web.core.mobile.service.BlueWallServiceImpl;

import java.util.Date;

@Controller
@RequestMapping(value = "mobile/blueWall")
public class BlueWallController {
	
	private static Logger logger = LoggerFactory.getLogger(BlueWallController.class);
	
	@Resource(name = "core.blueWallServiceImpl")
	private BlueWallServiceImpl service;
	
	
	/**
	 * 分页查询
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/getPageRows")
	public ResponseEntity<ResponseDto> getPageRows(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		BlueWallRequest request = JsonEntityTransform.json2Object(request_json, BlueWallRequest.class);
		logger.info("转换为obj之后的参数: {}", request.toString());
		//request.setLimit(999);
		PagingResult<TabCoreBlueWallEntity> pag = service.list(request);
		return new ResponseEntity<ResponseDto>(ResponseDto.responseOK(pag), HttpStatus.OK);
	}
	
	/**
	 * 添加
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/toAdd", method=RequestMethod.POST)
	public ResponseEntity<ResponseDto> add(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		TabCoreBlueWallEntity entity = JsonEntityTransform.json2Object(request_json, TabCoreBlueWallEntity.class);
		logger.info("转换为obj之后的参数: {}", entity.toString());
		if(entity.getLevel() == 2 || entity.getLevel() == 3){

			int i = 0;
			if(!Check.isNullObj(entity)){
				entity.setCreateTime(new Date());
				entity.setIsDelete(0);
				i = service.add(entity);
				logger.info("添加之后的json串:{}", JsonEntityTransform.object2Json(entity));
			}
			if (i>0) {
				return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
			}
		}
    	return new ResponseEntity<>(ResponseDto.responseFail("权限不够，添加失败"),HttpStatus.OK);
	}
	
	/**
	 * 修改
	 * @param req
	 * @param request_json
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method=RequestMethod.POST)
	public ResponseEntity<ResponseDto> update(HttpServletRequest req, String request_json){
		logger.info("传入参数: {}", request_json);
		TabCoreBlueWallEntity entity = JsonEntityTransform.json2Object(request_json, TabCoreBlueWallEntity.class);
		logger.info("转换为obj之后的参数: {}", entity.toString());

		entity.setModifyTime(new Date());
		int i = 0;
		if(!Check.isNullObj(entity)){
			i = service.update(entity);
			logger.info("更新之后的json串:{}", JsonEntityTransform.object2Json(entity));
		}
		if (i>0) {
    		return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
    	}
    	return new ResponseEntity<>(ResponseDto.responseFail(""),HttpStatus.OK);
	}
	
	/**
	 * 删除
	 * @param req
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method=RequestMethod.POST)
	public ResponseEntity<ResponseDto> delete(HttpServletRequest req, String request_json) {
		logger.info("传入参数: {}", request_json);
		String str = req.getParameter("ids");
		int i = 0;
		if(!Check.isNullStrStrict(str)){
			String[] strs = str.split(",");
			for (int j = 0; j < strs.length; j++) {
				int id = Integer.valueOf(strs[i]);
				i += service.delete(id);
			}
		}
		
		if (i>0) {
    		return new ResponseEntity<>(ResponseDto.responseOK(""), HttpStatus.OK);
    	}
    	return new ResponseEntity<>(ResponseDto.responseFail(""),HttpStatus.OK);
	}

}
